package com.motorhome.manage.service.impl;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.util.JsonUtil;
import com.motorhome.base.util.Result;
import com.motorhome.manage.common.statics.ManageStatics;
import com.motorhome.manage.dao.OrderDao;
import com.motorhome.manage.domain.Order;
import com.motorhome.manage.domain.PointResult;
import com.motorhome.manage.service.OpenService;
import com.motorhome.manage.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: John
 */
@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private OpenService openService;

    @Override
    public Pager<Order> getOrderList(String page, String rows) {
        return orderDao.getOrderList(Integer.parseInt(page), Integer.parseInt(rows));
    }

    @Override
    public Result deleteOrder(String id) {
        orderDao.deleteOrder(Long.parseLong(id));
        return new Result(0, "删除成功");
    }

    @Override
    public Result updateOrderStatus(String id, String status) {
        orderDao.updateOrderStatus(Long.parseLong(id), Integer.parseInt(status));
        return new Result(0, "更新状态成功");
    }

    @Override
    public Result updateMemberPoint(String id) {
        Order order = orderDao.getOrderById(Long.parseLong(id));
        //100积分1元
        String result = openService.updateMemberPoint(ManageStatics.OPEN_USER_ACCOUNT, order.getUserCardId(), 0 - order.getTotal() * 100, "微信管理后台扣费同步");
        if (result != null) {
            PointResult pointResult = JsonUtil.fromJson(result, PointResult.class);
            if (pointResult != null) {
                if (pointResult.getStatus() == 0) {
                    orderDao.updateOrderPoint(id, pointResult.getMessage());
                    return new Result(0, "扣积分成功");
                } else {
                    return new Result(1, pointResult.getMessage());
                }
            }
        }
        return new Result(1, "扣积分失败");
    }

}
